import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useMemberStore = defineStore(
  'user',
  () => {
    // 用户信息
    const userInfo = ref()
    const token = ref()
    const im = ref()
    const i = ref()
    const islogin = ref(0)
    const newi = ref()
    const casualUser = ref()
    const recordAvatar = ref()
    const echoImg = ref([])
    const recordImg = ref([])
    const setEchoImg = (val: any) => {
      echoImg.value = val
    }
    const setRecordImg = (val: any) => {
      recordImg.value = val
    }
    const setUserInfo = (val: any) => {
      userInfo.value = val
    }
    const setclose = (is: any) => {
      i.value = is
    }
    const setnewclose = (is: any) => {
      newi.value = is
    }
    const setCasualUser = (val: any) => {
      casualUser.value = val
    }
    const setToken = (val: any) => {
      token.value = val
    }
    const setIM = (val: any) => {
      im.value = val
    }
    const clearUserInfo = () => {
      userInfo.value = undefined
      im.value = undefined
    }
    const clearEchoImg = () => {
      echoImg.value.splice(0, echoImg.value.length)
    }
    const clearRecordImg = () => {
      recordImg.value.splice(0, recordImg.value.length)
    }
    const clearToken = () => {
      token.value = ''
    }
    const setislogin = (data) => {
      islogin.value = data
    }

    return {
      im,
      token,
      userInfo,
      setUserInfo,
      clearUserInfo,
      clearToken,
      setclose,
      i,
      newi,
      setnewclose,
      setToken,
      setIM,
      setCasualUser,
      casualUser,
      echoImg,
      setEchoImg,
      clearEchoImg,
      recordAvatar,
      recordImg,
      setRecordImg,
      clearRecordImg,
      islogin,
      setislogin,
    }
  },
  {
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key: any, value: any) {
          uni.setStorageSync(key, value)
        },
        getItem(key: any) {
          return uni.getStorageSync(key)
        },
      },
    },
  },
)
